package com.company.project.aop;

import com.alibaba.fastjson.JSON;
import com.company.project.utils.LoggerUtils;
import com.company.project.utils.RequestUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * IntelliJ IDEA.
 * User: lwb
 * Date: 2017/10/18
 * Time: 14:50
 * function:
 */
@Aspect
@Component
public class AroundAspectMv {

    @Around("execution(* com.company.project.controller..*.*(..)) " +
            "&& !@annotation(org.springframework.web.bind.annotation.ResponseBody)" +
            "&& @annotation(org.springframework.web.bind.annotation.RequestMapping)")//注解类型也必须是全限定类型名
    public Object aroundMethod(ProceedingJoinPoint point)
    {
        Object obj = null;
        String methodName = point.getSignature().getName();
        try
        {
            //入参数输出
            Map<String , String> map = RequestUtil.getParameterMap();
            LoggerUtils.info(methodName + "--->:" + JSON.toJSONString(map));

            obj =  point.proceed();
            if(obj instanceof ModelAndView)
            {
                //结果输出
                ModelAndView mv = (ModelAndView)obj;
                LoggerUtils.info(  "return --->:" + JSON.toJSONString(mv));
            }


        }catch (Throwable e)
        {
            LoggerUtils.error(e.getMessage() , e);
            if(obj instanceof ModelAndView)
            {
                //结果输出
                ModelAndView mv = (ModelAndView)obj;
                mv.setViewName("error");
            }
        }

        return obj;
    }


}
